package com.bbu.javaexam.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bbu.javaexam.domain.FillQuestion;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author Alex
 * @version 1.0.0
 * @ClassName FillQuestionMapper.java
 * @Description TODO
 */
@Mapper
public interface FillQuestionMapper extends BaseMapper<FillQuestion> {
    /**
     * @throws
     * @title 不通过分页查询所有填空题
     * @description
     * @author Alex
     * @updateTime 2021/12/5 14:42
     */
    @Select("select * from fill_question where questionId in (select questionId from paper_manage where questionType = 2 and paperId = #{paperId})")
    List<FillQuestion> findByIdAndType(Integer paperId);

    /**
     * @throws
     * @title 通过分页查询所有填空题
     * @description
     * @author Alex
     * @updateTime 2021/12/5 14:42
     */
    @Select("select * from fill_question")
    IPage<FillQuestion> findAll(Page page);

    /**
     * @throws
     * @title 查询最后一条id
     * @description
     * @author Alex
     * @updateTime 2021/12/5 14:42
     */
    @Select("select questionId from fill_question order by questionId desc limit 1")
    FillQuestion findOnlyQuestionId();

    /**
     * @throws
     * @title 新增一题填空题
     * @description
     * @author Alex
     * @updateTime 2021/12/5 14:43
     */
    @Options(useGeneratedKeys = true, keyProperty = "questionId")
    @Insert("insert into fill_question(subject,question,answer,analysis,level,section) values " +
            "(#{subject},#{question},#{answer},#{analysis},#{level},#{section})")
    int add(FillQuestion fillQuestion);

    /**
     * @throws
     * @title 查询指定科目，页的题目
     * @description
     * @author Alex
     * @updateTime 2021/12/5 14:43
     */
    @Select("select questionId from fill_question where subject = #{subject} order by rand() desc limit #{pageNo}")
    List<Integer> findBySubject(String subject, Integer pageNo);
}
